use Modern::Perl;
#use Smart::Comments;
use IO::All;
use Getopt::Long;

my $file;my $output;
GetOptions(
  "file|f=s" => \$file,
  "out|o=s" =>  \$output,
  "help|?"  => \&usage,
);

&usage unless $file;
$output = "$file.gnu.dat" unless $output;
#my $file = '15SA0571RZ_L8_I328.bL20000.nB1.cn0.05.chr1.plot.txt';
my @content = io($file)->chomp->slurp;
### Original data
my @x = split /\s+/, $content[0];
my $log2 = NA2undef([ split /\s+/, $content[1] ]);
my $log2_smoothed = NA2undef([ split /\s+/, $content[2] ]);
my $is_sexual = $content[7];
my $na_region = h_annotate(\@x, [sort{$a<=>$b}split /\s+/, $content[5]], $is_sexual);
my $data_base_isna_regions = h_annotate(\@x, [sort{$a<=>$b}split /\s+/, $content[6]], $is_sexual);
my $centromere = h_annotate(\@x, [split /\s+/, $content[8]], $is_sexual);
my $reported_upper = [split /\s+/, $content[10]];
my $reported_lower = [split /\s+/, $content[11]];
my $zct = NA2undef([ split /\s+/, $content[3] ]);
my $z_smoothed = NA2undef([ split /\s+/, $content[4] ]);
### Generate file
my $outfile = io($output);
"">$outfile;
map {
  my $i = $_;
    $x[$i]."\t".
    undef2NA($log2->[$i])."\t".
    undef2NA($log2_smoothed->[$i])."\t".
    undef2NA($zct->[$i])."\t".
    undef2NA($z_smoothed->[$i])."\t".
    undef2NA($na_region->[$i])."\t".
    undef2NA($data_base_isna_regions->[$i])."\t".
    undef2NA($centromere->[$i])."\t".
    undef2NA($reported_upper->[$i])."\t".
    undef2NA($reported_lower->[$i])."\n">>$outfile;
} (0..$#x);





#############################
# Some function
#############################
sub undef2NA {
  my $data = shift;
  $data
    ? $data
    : defined($data) && ($data == 0)
      ? 0
      : 'NA';
}
sub NA2undef {
  my $array = shift;
  [map{
    $_
      ? $_ =~ /NA/
        ? undef
        : $_
      : defined($_) && ($_ ==0)
        ? 0
        : undef;
      
  } @{$array}];
}

sub h_annotate{
  # $x: all x values
  # $position: an array ref
  # $value: y-value
  my ($x,$position,$value) = @_;
  my $x_length = scalar(@{$x});
  my $ret = [(undef)x$x_length];
  my $start_position = undef;
  for my $index (0..$#{$position}) {
    if ($index%2 == 0) {
      $start_position = $position->[$index];
    } else {
      my $end_position = $position->[$index];
      my @len = ($start_position..$end_position);
      if (scalar(@len)>7) {
        map {
          $ret->[$_-1] = $value;
        }@len;
      }
    }
  }
  $ret;
}

sub usage {
  my $USAGE = <<"USAGE";;
Description:  Transfer plot data for gnuplot
Version:      0.0.1
Example:        $0 -f <plot file>
Usage:
    --file|-f       : Plot file which stores data generated by BerryCNV. e.g. *.Chr*.plot.txt
    --out|-o        : Output file which stores data used by gnuplot. Default is generated by file option. 
                      e.g. if file option is *.Chr*.plot.txt, then output file is *.Chr*.plot.txt.gnu.dat
    --help          : Print help
USAGE
  print $USAGE;
  exit;

}

###################################
# 说明
#
# 输出文件格式如下
#
# column 1:   x轴坐标
# column 2:   log2
# column 3:   平滑后log2
# column 4:   z score
# column 5:   smoothed z score
# column 6:   测序未探测区域,0代表x位置未探测,NA代表x位置探测
# column 7:   数据没有的区域,0代表x位置未探测,NA代表x位置探测
# column 8:   着丝粒区域,0代表着丝粒区域,NA代表非着丝粒区域
# column 9:   报出的upper区域,对应于x轴坐标,NA无意义
# column 10:  报出的lower区域,对应于x轴坐标,NA无意义
